package cn.tedu.tmall.admin.mall.controller;

import cn.tedu.tmall.admin.mall.pojo.param.GoodsAddNewParam;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsListItemVO;
import cn.tedu.tmall.admin.mall.service.IGoodsService;
import cn.tedu.tmall.admin.mall.service.impl.GoodsService;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/goods")
@Validated
@Api(tags = "2. 商品管理")
@Slf4j
public class GoodsController {
    @Autowired
    IGoodsService service;

    @PostMapping("/add-new")
    @ApiOperation("发布商品")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                             @Validated GoodsAddNewParam addNewParam){

        service.add(currentPrincipal, addNewParam);
        return JsonResult.ok();
    }

    // TODO 根据ID删除商品
    // TODO 推荐商品
    // TODO 取消推荐商品

    @PostMapping("/{id:[0-9]+}/check-state/pass")
    @PreAuthorize("hasAuthority('/mall/goods/check')")
    @ApiOperation("审核通过商品")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "remarks", value = "备注信息", required = true, paramType = "query", dataType = "string")
    })
    public JsonResult passCheck(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id,
                                @NotBlank(message = "请提交审核备注信息") String remarks) {
        log.debug("开始处理【审核通过商品】的请求，参数：{}", id);
        service.passCheck(currentPrincipal, id, remarks);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/check-state/reject")
    @PreAuthorize("hasAuthority('/mall/goods/check')")
    @ApiOperation("拒绝审核商品")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "remarks", value = "备注信息", required = true, paramType = "query", dataType = "string")
    })
    public JsonResult rejectCheck(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                  @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id,
                                  @NotBlank(message = "请提交审核备注信息") String remarks) {
        log.debug("开始处理【拒绝审核商品】的请求，参数：{}", id);
        service.rejectCheck(currentPrincipal, id, remarks);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/put-on")
    @PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperation("上架商品")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult putOn(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                            @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
        log.debug("开始处理【上架商品】的请求，参数：{}", id);
        service.putOn(currentPrincipal, id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/pull-off")
    @PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperation("下架商品")
    @ApiOperationSupport(order = 321)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult pullOff(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                              @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
        log.debug("开始处理【下架商品】的请求，参数：{}", id);
        service.pullOff(currentPrincipal, id);
        return JsonResult.ok();
    }

    // TODO 根据ID查询商品

    @GetMapping("")
    @ApiOperation("查询商品列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码",dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "条数",dataType = "int")
    }
    )
    public JsonResult list(@Range(min = 1,message = "请提交有效的商品页码值") Integer pageNum,
                           @Range(min = 1,message = "请提交有效的商品条数") Integer pageSize){
        Integer pageNumm=pageNum==null ? 1 : pageNum;
        Integer pageSizee=pageNum==null ? 1 : pageSize;
        PageData<GoodsListItemVO> list = service.list(pageNumm,pageSizee);
        return JsonResult.ok(list);
    }

    @GetMapping("/list-by-category")
    @ApiOperation("根据类别查询商品列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类别ID",dataType = "long"),
            @ApiImplicitParam(name = "pageNum", value = "页码",dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "条数",dataType = "int")
    }
    )
    public JsonResult listByCategory(@Range(message = "请提交有效的商品类别ID") Long categoryId,
                                     @Range(min = 1,message = "请提交有效的商品页码值") Integer pageNum,
                                     @Range(min = 1,message = "请提交有效的商品条数") Integer pageSize){
        Integer pageNumm=pageNum==null ? 1 : pageNum;
        Integer pageSizee=pageNum==null ? 1 : pageSize;
        PageData<GoodsListItemVO> list = service.listByCategory(categoryId,pageNumm,pageSizee);
        return JsonResult.ok(list);

    }


}
